package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hexinfo.dmpro.common.service.MessageCenterService;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import com.hexinfo.dmpro.sparing.dto.*;
import com.hexinfo.dmpro.sparing.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class RuleToDistCPServiceImpl implements RuleToDistCPService {

    private final MessageCenterService messageCenterService;
    private final BackupSourceService backupSourceService;
    private final BackupTimeService backupTimeService;
    private final HdfsService hdfsService;
    private final HandBackupService handBackupService;
    private final DropDateDataService dropDateDataService;
    private WarnService warnService;


    /**
     * 热备
     */
    @Override
    public String heatBackup(@Valid RuleToDistCPDTO ruleToDistCPDTO,String logId) {
        log.info("----------------------------热备数据开始！！！！--------------------");
//        Logger.log("--------------------------------数据热备开始----------------------------------------");
        //1、获取上次热备Date
        Date date = messageCenterChange(ruleToDistCPDTO.getSourceDataName());
        ruleToDistCPDTO.setDate(date);
//        Logger.log("上次热备时间："+date+"--有无源类型："+ruleToDistCPDTO.getBackupSort());
//        //2、有无源分类(1：有源分区字段，2：无源分区字段（时间窗口）)
//        if (ruleToDistCPDTO.getBackupSort().equals("1")){
//            backupSourceService.execShell(ruleToDistCPDTO);
//        }else {
//            backupTimeService.execShell(ruleToDistCPDTO);
//        }
        boolean execShell = backupSourceService.execShell(ruleToDistCPDTO,logId);
        if (execShell){
            return "";
        }else {
            //发送失败源情况告警
//            warnService.warnSourceFail(logId);
            return "异常";
        }
    }

    /**
     * 万国热备到UAT
     */
    @Override
    public String heatBackupWgToUat(RuleToDistCPDTO ruleToDistCPDTO) {
        try {
            String  str = backupSourceService.execShellWgToUat(ruleToDistCPDTO);
            return str;
        } catch (Exception e) {
            return "异常";
        }
    }


    /**
     * 手动同步
     */
    @Override
    @Async
    public String handBackup(TableHandDTO tableHandDTO) {
        log.info("----------------------------手动同步开始！！！！--------------------");
        handBackupService.handBackup(tableHandDTO);
        return "手动同步结束";
    }

    /**
     * 测试同步
     */
    @Override
    @Async
    public String hdfsDistcp(List<TbHdfsDistcpDTO> tbHdfsDistcpDTOs,String fileName,String id) {
        log.info("----------------------------数据distcp开始！！！！--------------------");
        hdfsService.hdfsDistcp(tbHdfsDistcpDTOs,fileName,id);
        return "测试同步结束";
    }

    /**
     * 删除两年前日期分区数据
     */
    @Override
    public boolean dropDateData(String source) {
        log.info("----------------------------删除两年前日期分区数据开始！！！！--------------------");
        return dropDateDataService.dropDateData(source);
    }

    /**
     * 获取上次热备Data
     * @return
     */
    public Date messageCenterChange(String dataName) {
        Date date = messageCenterService.maxCreateTime(
                ScanCommonConstants.ClusterName.YSJHQ.value +dataName);
        String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
        return DateUtil.parse(format);
    }

}
